#include "UdpServer.hpp"
#include <memory>
#include <iostream>

using namespace std;

extern Log lg;

void Usage(std::string proc)
{
    std::cout << "\n\rUsage: " << proc << " serverip serverport\n" << std::endl;
}

// ./udpclient serverip serverport
int main(int argc, char *argv[])
{
    if(argc != 3)
    {
        Usage(argv[0]);
        exit(0);
    }
    std::string serverip = argv[1];
    uint16_t serverport = std::stoi(argv[2]);

    // 确认发送服务端
    struct sockaddr_in server;
    bzero(&server, sizeof(server));
    server.sin_family = AF_INET;
    server.sin_port = htons(serverport);
    server.sin_addr.s_addr = inet_addr(serverip.c_str());
    socklen_t len = sizeof(server);

    int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    if(sockfd < 0)
    {
        cout << "socker error" << endl;
        return 1;
    }
    lg(Info, "socket create success, sockfd: %d", sockfd);
    // client要bind吗?——要，只不过不需要用户显示的bind!一般由 OS 自由随机选择!
    //一个端口号只能被一个进程bind，对server是如此，对于client，也是如此!
    //其实client的port是多少，其实不重要，只要能保证主机上的唯一性就可以!
    //系统什么时候bind呢?——首次发送数据的时候

    string message;
    char buffer[1024];
    while(true)
    {
        // 1. 获取数据
        cout << "Please Enter: ";
        getline(cin, message);
        
        // 2. 给服务端发送信息
        sendto(sockfd, message.c_str(), message.size(), 0, (struct sockaddr*)&server, len);

        struct sockaddr_in tmp;
        socklen_t t_len = sizeof(tmp);

        ssize_t n = recvfrom(sockfd, buffer, 1023, 0, (struct sockaddr*)&tmp, &t_len);
        if (n > 0)
        {
            buffer[n] = 0;
            cout << buffer << endl;
        }
    }

    close(sockfd);
    return 0;
}